package com.project.internship.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.internship.entity.Bases;
import com.project.internship.mapper.BasesMapper;
import com.project.internship.service.BasesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.internship.utils.UserUtils;
import com.project.internship.utils.YearUtils;
import com.project.internship.vo.BasesDeptVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2020-04-07
 */
@Service
public class BasesServiceImpl extends ServiceImpl<BasesMapper, Bases> implements BasesService {

    @Autowired
    BasesMapper basesMapper;


    @Override
    public IPage<BasesDeptVO> getAllBases(Page<BasesDeptVO> page,String keyword) {
        return basesMapper.getAllBases(page,keyword);
    }

    @Override
    public Integer addBase(Bases base) {
        base.setDepartmentId(UserUtils.getCurrentUser().getDepartmentId());
        return basesMapper.insert(base);
    }

    @Override
    public Integer updateBase(Bases base) {
        return basesMapper.updateById(base);
    }

    @Override
    public Integer deleteBase(Long id) {
        return basesMapper.deleteById(id);
    }

    @Override
    public Integer deleteBases(Long[] ids) {
        return basesMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public IPage<Bases> getAllOwnBases(Page<Bases> page, String keyword) {
        return basesMapper.getAllOwnBases(page, UserUtils.getCurrentUser().getDepartmentId(),keyword);
    }

    @Override
    public List<Bases> getNonAllotBases() {
        return basesMapper.getNonAllotBases(UserUtils.getCurrentUser().getDepartmentId(), YearUtils.getCurrentYear());
    }

    @Override
    public IPage<Bases> getAllotBases(Page<Bases> page, String keyword) {
        return basesMapper.getAllotBases(page, keyword, UserUtils.getCurrentUser().getDepartmentId(), YearUtils.getCurrentYear());
    }

    @Override
    public List<BasesDeptVO> getBaseSelf() {
        return basesMapper.getBaseSelf(UserUtils.getCurrentUser().getBaseId());
    }

    @Override
    public Integer updateBaseInfo(BasesDeptVO basesDeptVO) {
        Bases base = new Bases();
        base.setId(basesDeptVO.getId());
        base.setName(basesDeptVO.getBname());
        base.setCity(basesDeptVO.getCity());
        base.setLocation(basesDeptVO.getLocation());
        base.setPhone(basesDeptVO.getPhone());

        return basesMapper.updateById(base);
    }

}
